
let timer = null
Page({
  onShareAppMessage() {
    return {
      title: 'input',
      path: 'pages/component/input/input'
    }
  },

  data: {
    show: true,
    focus: false,
    inputValue: '',
    updateValue: '',
    typeArray: ['text', 'number', 'idcard', 'digit'],
    typeIndex: 0,
    confirmTypeArray: ['send', 'search', 'next', 'go', 'done'],
    confirmTypeIndex: 0,
    boolArray: [true, false],
    boolIndex: 0,
    cursorValue: 0,
    cursorValueTemp: 0,
    selectionStartValue: 0,
    selectionEndValue: 0,

    bindFocusFlag:'否',
    bindBlurFlag:'否',
    bindConfirmFlag:'否',
    bindKeyboardHeightChangeFlag:'否',

    activeCursorInput:false,
    activeSelectionInput:false
  },

  onLoad() {
    timer = setInterval(() => {
      this.setData({
        active: !this.data.active
      })
    }, 3000)
  },

  onUnload() {
    timer && clearInterval(timer)
  },

  toggleInputShow() {
    this.setData({
      show: !this.data.show
    })
  },

  toggleInputValue() {
    this.setData({
      updateValue: String(Math.random()).slice(0, 12)
    })
  },

  bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  bindReplaceInput(e) {
    const value = e.detail.value
    let pos = e.detail.cursor
    let left
    if (pos !== -1) {
      // 光标在中间
      left = e.detail.value.slice(0, pos)
      // 计算光标的位置
      pos = left.replace(/11/g, '2').length
    }

    // 直接返回对象，可以对输入进行过滤处理，同时可以控制光标的位置
    return {
      value: value.replace(/11/g, '2'),
      cursor: pos
    }

    // 或者直接返回字符串,光标在最后边
    // return value.replace(/11/g,'2'),
  },

  bindHideKeyboard(e) {
    if (e.detail.value === '123') {
      // 收起键盘
      mp.hideKeyboard()
    }
  },

  bindTypePickerChange: function(e) {
    console.log('bindTypePickerChange值为', e.detail.value)
    this.setData({
      typeIndex: e.detail.value
    })
  },

  bindConfirmTypePickerChange: function(e) {
    console.log('bindConfirmTypePickerChange值为', e.detail.value)
    this.setData({
      confirmTypeIndex: e.detail.value
    })
  },

  bindBoolPickerChange: function(e) {
    console.log('bindBoolPickerChange值为', e.detail.value)
    this.setData({
      boolIndex: e.detail.value
    })
  },

  bindFocusAction: function(e) {
    console.log('bindFocusAction值为', e.detail.value)
    this.setData({
      bindFocusFlag:'收到focus事件',
    })
  },

  bindblurAction: function(e) {
    console.log('bindblurAction值为', e.detail.value)
    this.setData({
      bindBlurFlag:'收到blur事件',
    })
  },

  bindconfirmAction: function(e) {
    console.log('bindconfirmAction值为', e.detail.value)
    this.setData({
      bindConfirmFlag:'收到confirm事件',
    })
  },
  
  bindkeyboardheightchangeAction: function(e) {
    console.log('bindkeyboardheightchange值为', e.detail.height)
    this.setData({
      bindKeyboardHeightChangeFlag:'收到keyboardheightchange事件',
    })
  },

  bindCursor:function(e) {
    console.log('bindCursor:', e.detail)
    console.log('bindCursor int:', parseInt(e.detail.value))
    this.setData({
      cursorValueTemp: parseInt(e.detail.value)
    })
  },

  bindSelectionStart:function(e) {
    this.setData({
      selectionStartValue: parseInt(e.detail.value)
    })
  },

  bindSelectionEnd:function(e) {
    this.setData({
      selectionEndValue: parseInt(e.detail.value)
    })
  },

  activeCursorInput:function(e) {
    console.log('Cursor:', this.data.cursorValueTemp)
    this.setData({
      activeCursorInput:true,
      cursorValue:this.data.cursorValueTemp
    })
  },

  activeSelectionInput:function(e) {
    this.setData({
      activeSelectionInput:true
    })
  }
})
